package cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanUpdateStatusReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.twcleaningplan.TwCleaningPlanDO;
import cn.iocoder.yudao.module.pplan.service.twcleaningplan.TwCleaningPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 生产计划-餐具清洗计划")
@RestController
@RequestMapping("/pplan/tw-cleaning-plan")
@Validated
public class TwCleaningPlanController {

    @Resource
    private TwCleaningPlanService twCleaningPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划-餐具清洗计划")
    @PreAuthorize("@ss.hasPermission('pplan:tw-cleaning-plan:create')")
    public CommonResult<Long> createTwCleaningPlan(@Valid @RequestBody TwCleaningPlanSaveReqVO createReqVO) {
        return success(twCleaningPlanService.createTwCleaningPlan(createReqVO));
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新机供品餐具清洗计划清洗状态")
    public CommonResult<Boolean> updateCleaningPlanStatus(@Valid @RequestBody TwCleaningPlanUpdateStatusReqVO updateReqVO) {
        twCleaningPlanService.updateCleaningPlanStatus(updateReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划-餐具清洗计划")
    @PreAuthorize("@ss.hasPermission('pplan:tw-cleaning-plan:update')")
    public CommonResult<Boolean> updateTwCleaningPlan(@Valid @RequestBody TwCleaningPlanSaveReqVO updateReqVO) {
        twCleaningPlanService.updateTwCleaningPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划-餐具清洗计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:tw-cleaning-plan:delete')")
    public CommonResult<Boolean> deleteTwCleaningPlan(@RequestParam("id") Long id) {
        twCleaningPlanService.deleteTwCleaningPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划-餐具清洗计划")
                @PreAuthorize("@ss.hasPermission('pplan:tw-cleaning-plan:delete')")
    public CommonResult<Boolean> deleteTwCleaningPlanList(@RequestParam("ids") List<Long> ids) {
        twCleaningPlanService.deleteTwCleaningPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划-餐具清洗计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pplan:tw-cleaning-plan:query')")
    public CommonResult<TwCleaningPlanRespVO> getTwCleaningPlan(@RequestParam("id") Long id) {
        TwCleaningPlanDO twCleaningPlan = twCleaningPlanService.getTwCleaningPlan(id);
        return success(BeanUtils.toBean(twCleaningPlan, TwCleaningPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划-餐具清洗计划分页")
    @PreAuthorize("@ss.hasPermission('pplan:tw-cleaning-plan:query')")
    public CommonResult<PageResult<TwCleaningPlanRespVO>> getTwCleaningPlanPage(@Valid TwCleaningPlanPageReqVO pageReqVO) {
        PageResult<TwCleaningPlanRespVO> pageResult = twCleaningPlanService.getTwCleaningPlanPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划-餐具清洗计划 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:tw-cleaning-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTwCleaningPlanExcel(@Valid TwCleaningPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TwCleaningPlanRespVO> list = twCleaningPlanService.getTwCleaningPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计划-餐具清洗计划.xls", "数据", TwCleaningPlanRespVO.class,
                        BeanUtils.toBean(list, TwCleaningPlanRespVO.class));
    }

}